package dsk.demo.product.controller.product;

import dsk.demo.common.utils.R;
import dsk.demo.product.entity.GuanggaoEntity;
import dsk.demo.product.service.GuanggaoService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

@RestController
public class GuanggaoController {

    @Autowired
    GuanggaoService guanggaoService;

    /**
     * 保存图片
     */
    @RequestMapping("/guanggao/image/{guanggaoId}")
    public R saveImage(MultipartFile file, @PathVariable Integer guanggaoId, HttpServletRequest req) {
        String type = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        String filename = UUID.randomUUID().toString() + type;

        //linux 系统中的  文件保存地址
        File fDir = new File(File.separator); //File.separator表示根目录，比如现在就表示在D盘下。
        String strFile = "mydata" + File.separator + "nginx" + File.separator + "html" + File.separator + "images" + File.separator;

        File file1 = new File(fDir, strFile + filename);
        String filePath = "";
        try {

            filePath = req.getScheme() + "://" + req.getServerName() + ":" +
                    req.getServerPort() + "/images/" + file1.getName();
            System.out.println(filePath);
            file.transferTo(file1);
//            productService.saveImage(productId,filePath);
            GuanggaoEntity entity = new GuanggaoEntity();
            entity.setId(guanggaoId);
            entity.setType("1");
            entity.setUrl(filePath);
            System.out.println(entity);
            guanggaoService.updateById(entity);
        } catch (IOException exception) {
            return R.error(500, "上传文件异常");
        }

        return R.ok().put("image", filePath);
    }


    /**
     * 新增
     */
    @RequestMapping("/guanggao/save")
    @RequiresPermissions("product:product:save")
    public R save() {
        try {
            GuanggaoEntity guanggaoEntity = new GuanggaoEntity();
            guanggaoEntity.setType("1");
            guanggaoService.save(guanggaoEntity);
        } catch (Exception e) {
            System.out.println(e.getLocalizedMessage());
            return R.error(501, "新增广告栏位失败");
        }
        return R.ok();
    }

    /**
     * 新增
     */
    @RequestMapping("/guanggao/list")
    @RequiresPermissions("product:product:save")
    @ResponseBody
    public R list(String type) {
        try {
            List<GuanggaoEntity> list = guanggaoService.list();
            return R.ok().put("data", list);
        } catch (Exception e) {
            return R.error(501, "新增广告栏位失败");
        }
    }

    /**
     * 新增
     */
    @RequestMapping("/guanggao/delete")
    @RequiresPermissions("product:product:save")
    @ResponseBody
    public R delete(@RequestBody Integer[] ids) {
        try {
            System.out.println(ids);
            guanggaoService.removeByIds(Arrays.asList(ids));
        } catch (Exception e) {
            return R.error(501, "删除广告栏位失败");
        }
        return R.ok();
    }

    /**
     * 新增
     */
    @RequestMapping("/guanggao/update")
    @RequiresPermissions("product:product:save")
    @ResponseBody
    public R update(@RequestBody GuanggaoEntity entity) {
        try {
            System.out.println(entity);
            guanggaoService.updateById(entity);
        } catch (Exception e) {
            return R.error(501, "删除广告栏位失败");
        }
        return R.ok();
    }


}
